<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>异步 AJAX(实例1)</title>
</head>

<body>
    <p><strong>以下内容是通过异步请求获取的：</strong></p>
    <p id="demo"></p>
    <script>
        var xhr = new XMLHttpRequest();

        xhr.onload = function () {
            // 输出接收到的文字数据
            document.getElementById("demo").innerHTML = xhr.responseText;
        }

        xhr.onerror = function () {
            document.getElementById("demo").innerHTML = "请求出错";
        }

        // 发送异步 GET 请求
        xhr.open("GET", "https://www.runoob.com/try/ajax/ajax_info.txt", true);
        xhr.send();
    </script>
</body>

</html>